import { defineStore } from 'pinia'
import { ref } from 'vue'
import { login as apiLogin } from '../api/auth'

export const useAuthStore = defineStore('auth', () => {
  const token = ref<string | null>(localStorage.getItem('token'))

  const login = async (username: string, password: string) => {
    const response = await apiLogin(username, password)
    token.value = response.access_token
    localStorage.setItem('token', response.access_token)
  }

  const logout = () => {
    token.value = null
    localStorage.removeItem('token')
  }

  return { token, login, logout }
})